#include<iostream>
#include <sys/types.h>        
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <signal.h>

using namespace std;

class Tcp_Server
{
public:
    Tcp_Server(int port)
        :_sockfd(-1)
        ,_port(port)
    {}


    void Tcp_server_init()
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(_sockfd < 0)
        {
            cerr << "socket error!" << endl;
            exit(2);
        }

        cout << "socket success!" << endl;

        struct sockaddr_in local;
        memset(&local, '\0', sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = INADDR_ANY;

        
        if(bind(_sockfd, (sockaddr*)&local, sizeof(local)) < 0)
        {
            cerr << "bind error!" << endl;
            exit(3);
        }

        cout << "bind success!" << endl;

        //Tcp需要监听; Tcp是面向连接, 查看客户端的连接请求
    #define BLOCKLOG 5
        if(listen(_sockfd, BLOCKLOG) < 0)
        {
            cerr << "listen error!" << endl;
            exit(4);
        }

        cout << "listen success!" << endl;
    }


    void Start()
    {
        signal(SIGCHLD, SIG_IGN);//屏蔽17号信号, 父进程无需等待子进程可以干自己的事情.
        for(;;)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            //获取连接请求的数据
            int sock = accept(_sockfd, (sockaddr*)&peer, &len);
            if(sock < 0)
            {
                cerr << "accept error, again!" << endl;
                continue;
            }

            //获取请求端的ip和port;
            string client_ip = inet_ntoa(peer.sin_addr);
            int client_port = ntohs(peer.sin_port);
            cout << "client ip is: " << client_ip << " " << "client port is: " << client_port << endl;

            //读取客户端消息;
            //Service(sock, client_ip, client_port);

            pid_t id = fork();
            if(id == 0)
            {
                //子进程;
                Service(sock, client_ip, client_port);
                exit(0);
            }
        }
    }

    void Service(int sock, string ip, int port)
    {
        char buff[1024];
        while(true)
        {
            ssize_t s = read(sock, buff, sizeof(buff)-1);
            if(s > 0)
            {
                buff[s] = '\0';
                cout << "get a new link!" << sock << "ip is: " << ip << "port is: " << port << buff <<  endl;
                write(sock, buff, s);
            }
            else if(s == 0)
            {
                //对端关闭;
                cout << "ip is: " << ip << "port is: " << port << "close!" << endl;
                break;
            }
            else
            {
                //读取失败;
                cerr << "read error!" << endl;
                break;
            }
        }
        close(sock);
        cout << "ip is: " << ip << "port is: " << port << " is done!" << endl;
    }

    ~Tcp_Server()
    {
        if(_sockfd > 0)
            close(_sockfd);
    }
private:
    int _sockfd;
    int _port;
};


void Usage(std::string proc)
{
	std::cout << "Usage: " << proc << " port" << std::endl;
}

int main(int argc, char* argv[])
{
	if (argc != 2)
    {
		Usage(argv[0]);
		exit(1);
	}

	int port = atoi(argv[1]);
	Tcp_Server* svr = new Tcp_Server(port);
	svr->Tcp_server_init();
	svr->Start();
	return 0;
}

